package com.quickask.dao;

import com.quickask.common.Pager;
import com.quickask.entity.AsknowQuestionLocationSetEntity;
import com.quickask.idao.IQuestionLocationSetDao;
import com.quickask.operate.data.Constants;
import com.quickask.operate.encode.Geohash;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by NaSa on 2015/10/2.
 */
public class QuestionLocationSetDao extends BaseDao<AsknowQuestionLocationSetEntity, Integer> implements IQuestionLocationSetDao {
    /**
     * 添加问题位置实体
     *
     * @param asknowQuestionLocationSetEntity
     */
    @Override
    public void addQuestionLocation(AsknowQuestionLocationSetEntity asknowQuestionLocationSetEntity) {
        save(asknowQuestionLocationSetEntity);
    }

    /**
     *
     * @param pageNo 页码
     * @param pageSize 页大小
     * @param latitude 纬度
     * @param longitude 经度
     * @param blockCode 区号
     * @return
     */
    @Override
    public Pager<AsknowQuestionLocationSetEntity> getQuestionPagers(int pageNo, int pageSize, double latitude, double longitude, String blockCode) {
        double minLatitude = latitude- Constants.LOCATE_DEVIATION;
        double maxLatitude = latitude+Constants.LOCATE_DEVIATION;
        double minLongitude = longitude- Constants.LOCATE_DEVIATION;
        double maxLongitude = longitude+Constants.LOCATE_DEVIATION;
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("minLatitude",minLatitude);
        properties.put("maxLatitude",maxLatitude);
        properties.put("minLongitude",minLongitude);
        properties.put("maxLongitude",maxLongitude);
        Pager<AsknowQuestionLocationSetEntity> pagers = findPagerByFetchedHql("from AsknowQuestionLocationSetEntity as aqlse where (aqlse.latitude between :minLatitude and :maxLatitude)" +
                        " and (aqlse.longitude between :minLongitude and :maxLongitude) order by aqlse.questionId desc",
                null, pageNo, pageSize, properties);

        return (pagers == null || pagers.getPageRecords().isEmpty())?findPagerByFetchedHql("from AsknowQuestionLocationSetEntity as aqlse where aqlse.blockCode = ? order by aqlse.questionId desc",
                null,pageNo,pageSize,blockCode):pagers;
    }

    /**
     * @param pageNo     页码
     * @param pageSize   页大小
     * @param properties (latitude,longitude,blockCode...)
     * @return
     */
    @Override
    public Pager<AsknowQuestionLocationSetEntity> getQuestionPagersByGeohash(int pageNo, int pageSize, Map<String, Object> properties) {
        String geohashStr = new Geohash().encode((Double)properties.get("latitude"),(Double)properties.get("longitude")).substring(0,Constants.GEO_STR_LENGTH);
        properties.put("geohash",geohashStr+ "%");
        System.out.println("geohash的值: "+geohashStr);
        Pager<AsknowQuestionLocationSetEntity> pagers = findPagerByFetchedHql("from AsknowQuestionLocationSetEntity as aqlse where aqlse.geohash like :geohash order by aqlse.questionId desc",
                null, pageNo, pageSize, properties);
        return (pagers == null || pagers.getPageRecords().isEmpty())?findPagerByFetchedHql("from AsknowQuestionLocationSetEntity as aqlse where aqlse.blockCode = :blockCode order by aqlse.questionId desc",
                null,pageNo,pageSize,properties):pagers;
    }


}
